package com.monkeygroup.mygoplus.view.entrance.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.view.entrance.activity.EntranceActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cumt on 2017/3/16.
 */

public class FindPasswordFragment extends Fragment implements View.OnClickListener {

    public static FindPasswordFragment getInstance(String phone) {
        FindPasswordFragment f = new FindPasswordFragment();
        Bundle bundle = new Bundle();
        bundle.putString("phone", phone);


        f.setArguments(bundle);

        return f;
    }

    private View mView;

    private Activity activity;
    //头部
    private TextView txtTitle;
    private ImageView imgBack;

    private EditText new_et;
    private TextView txt_find_pwd_notice, new_tv, btn_change_pwd;


    // Fragment管理对象
    private FragmentManager manager;
    private FragmentTransaction ft;
    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    private String phone;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        activity = this.getActivity();
        manager = getFragmentManager();

        if (mView == null) {

            mView = inflater.inflate(R.layout.fragment_findpassword, container, false);


            initView();
            initListener();
            initRequest();
        }
        return mView;
    }

    private void initView() {

        txtTitle = (TextView) mView.findViewById(R.id.txt_title);
        txtTitle.setText("忘記密碼");
        imgBack = (ImageView) mView.findViewById(R.id.img_back);

        new_et = (EditText) mView.findViewById(R.id.new_et);
        txt_find_pwd_notice = (TextView) mView.findViewById(R.id.txt_find_pwd_notice);
        new_tv = (TextView) mView.findViewById(R.id.new_tv);
        btn_change_pwd = (TextView) mView.findViewById(R.id.btn_change_pwd);


        params = new HashMap<>();
        phone = getArguments().getString("phone");

    }

    private void initListener() {

        mView.findViewById(R.id.btn_change_pwd).setOnClickListener(this);

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });

        new_et.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean booleanRight = false;
                if (TextUtils.isEmpty(s)) {
                    new_tv.setVisibility(View.VISIBLE);
                } else {

                    int flag = 0;
                    new_tv.setVisibility(View.GONE);
                    if (new_et.length() < 6) {
                        flag = 1;
                    }

                    if (!VilidateUtil.isPwd(String.valueOf(new_et.getText()))) {

                        flag = 2;
                    }

                    if (flag > 0) {

                        if (flag == 1) {
                            txt_find_pwd_notice.setVisibility(View.VISIBLE);
                            txt_find_pwd_notice.setText("至少要6碼");
                        }
                        if (flag == 2) {
                            txt_find_pwd_notice.setVisibility(View.VISIBLE);
                            txt_find_pwd_notice.setText("字母+數字");
                        }
                        booleanRight = true;
                    } else {
                        booleanRight = false;
                        txt_find_pwd_notice.setVisibility(View.INVISIBLE);
                    }

                }

                if (!booleanRight) {

                    btn_change_pwd.setBackgroundResource(R.drawable.btn_red_shape);

                } else {
                    btn_change_pwd.setBackgroundResource(R.drawable.btn_grey_shape);
                }

            }


            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    public void initRequest() {

        httpModel = new HttpModel(RequestUrl.baseUrl + RequestUrl.findPwdUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (!VilidateUtil.isSuccess(resultCode)) {

                    MyAlertDialog dialog = new MyAlertDialog(activity, resultMsg, "");
                    dialog.show(activity, resultMsg, 1);
                } else {
                    ((EntranceActivity) activity).initECLoginConfig(phone);

                    MyAlertDialog dialog = new MyAlertDialog(activity, "找回成功", "");
                    dialog.show(activity, "找回成功", 1);
                }
            }
        }, true);


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_change_pwd:

                params.put("mobile", phone);
                params.put("confirmPassword", String.valueOf(new_et.getText()));
                params.put("password", String.valueOf(new_et.getText()));
                httpModel.startRequest();
                break;

        }

    }
}
